﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASoft.Msg;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using ASoft.Data;
using System.Drawing;
using System.Windows.Media;
using System.Threading;
using System.Threading.Tasks;

namespace ASoft.WPF.ViewModel
{
    public partial class RoomDataViewModel
    {
        RoomData roomData;
        public RoomDataViewModel(RoomData roomData)
        {
            this.roomData = roomData;
            this.Id = roomData.Id;
            this.RoomName = roomData.RoomName;
            this.RoomGroupName = roomData.RoomGroupName;
            this.RoomTree = new TreeViewModel();
            this.RoomTree.IsChangedChild = true;

            this.温室实时数据 = new RoomReturnSearchMsgViewModel();
            this.温室实时数据.Id = this.Id;
            this.强制动作参数 = new RoomSetActionMsgViewModel();

            温室设置数据 = new RoomSetBaseViewModel();
            var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.Id);
            WPFUtility.TransFromDataToViewModel(roomSetMsg, 温室设置数据);

            WPFUtility.TransFromDataToViewModel(roomData, this);
        }



        public RoomReturnSearchMsgViewModel 温室实时数据 { get; private set; }

        public RoomSetActionMsgViewModel 强制动作参数 { get; private set; }


        public ICommand 强制动作参数命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        var msg = new RoomSetActionMsg();
                        msg.Id = this.Id;
                        WPFUtility.TransFromViewModelToData(强制动作参数, msg);
                        var returnMsg = RC.Current.MsgService.TransSendMsg(msg) as RoomReturnSetActionMsg;

                        if (returnMsg == null)
                        {
                            TagString = "设置设备强制动作参数失败";
                            return;
                        }
                        if (returnMsg.状态 == 0)
                        {
                            TagString = "设备返回状态为:失败";
                        }
                        else if (returnMsg.状态 == 1)
                        {
                            TagString = "设备返回状态为:成功";
                        }

                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }


        public DateTime 实时数据最后更新时间 { get; set; }

        #region 温室设置数据

        public RoomSetBaseViewModel 温室设置数据 { get; private set; }

        public ICommand 温室设置数据保存命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.Id);
                        WPFUtility.TransFromViewModelToData(温室设置数据, roomSetMsg);
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }

                        return IsDataChanged && !RC.Current.IsMonitoring;
                    });
            }
        }

        bool IsDataChanged
        {
            get
            {
                var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.Id);
                return !WPFUtility.DataEqualsViewModel(roomSetMsg, 温室设置数据);
            }
        }

        public ICommand 温室设置数据读设备命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        var msg = new RoomReadMsg();
                        msg.Id = this.Id;
                        var returnMsg = RC.Current.MsgService.TransSendMsg(msg) as RoomReturnReadMsg;
                        if (returnMsg == null)
                        {
                            TagString = "读设备失败";
                            return;
                        }
                        TagString = "读设备成功";
                        WPFUtility.TransFromDataToViewModel(returnMsg, this.温室设置数据);
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        public ICommand 温室设置数据写设备命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        var msg = new RoomSetMsg();
                        msg.Id = this.Id;
                        WPFUtility.TransFromViewModelToData(this.温室设置数据, msg);
                        var returnMsg = RC.Current.MsgService.TransSendMsg(msg) as RoomReturnSetMsg;
                        if (returnMsg == null || returnMsg.状态 == 0)
                        {
                            TagString = "写设备成功";
                        }
                        else
                        {
                            TagString = "写设备失败";
                        }
                        //Messenger.Default.Send<MessageBase>(new MessageBase(), "NotImp");
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        #endregion

        #region 湿帘泵关闭

        public ICommand 湿帘泵合拢命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        SendHandMsg("手动合拢湿帘泵", (msg) =>
                        {
                            msg.湿帘泵手动自动标志 = 0;
                            msg.湿帘泵手动运行方向 = 1;
                        });
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        public ICommand 湿帘泵展开命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        SendHandMsg("手动展开湿帘泵", (msg) =>
                        {
                            msg.湿帘泵手动自动标志 = 0;
                            msg.湿帘泵手动运行方向 = 0;
                        });
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        void SendHandMsg(string msgMemo, Action<RoomSetMsg> roomSetMsg)
        {
            var setMsg = RC.Current.RootData.温室设置信息集合.SingleOrDefault(p => p.Id == this.Id);
            var msg = setMsg.Clone() as RoomSetMsg;
            roomSetMsg(msg);
            if (!RC.Current.MsgService.SerialPortService.IsOpen)
            {
                RC.Current.MsgService.SerialPortService.OpenPort();
            }
            RC.Current.MsgService.SendMsg(msg);
            TagString = msgMemo;
            Messenger.Default.Send<MessageBase>(new MessageBase(), msgMemo);
        }

        public ICommand 灌溉打开命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        SendHandMsg("手动打开灌溉", (msg) =>
                        {
                            msg.灌溉加湿电磁阀手动自动标志 = 0;
                            msg.灌溉加湿电磁阀手动运行方向 = 1;
                        });
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        public ICommand 灌溉关闭命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        SendHandMsg("手动关闭灌溉", (msg) =>
                        {
                            msg.灌溉加湿电磁阀手动自动标志 = 0;
                            msg.灌溉加湿电磁阀手动运行方向 = 0;
                        });
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !RC.Current.IsMonitoring;
                    });
            }
        }

        #endregion




        #region "TagString"

        private string _TagString = default(string);

        public string TagString
        {
            get
            {
                return _TagString;
            }
            set
            {
                if (_TagString == value)
                {
                    return;
                }

                var oldValue = _TagString;
                _TagString = value;

                RaisePropertyChanged("TagString");
            }
        }

        #endregion


        #region RoomData

        public ICommand 温室基本数据保存命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        WPFUtility.TransFromViewModelToData(this, roomData);
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return IsRoomDataChanged && !RC.Current.IsMonitoring;
                    });
            }
        }

        bool IsRoomDataChanged
        {
            get
            {
                return !WPFUtility.DataEqualsViewModel(roomData, this);
            }
        }

        #endregion

        public string Title
        {
            get
            {
                return roomData.ToString();
            }
        }

        #region Position

        #region "Left"

        public Double Left
        {
            get
            {
                return PositionRoom.Left;
            }
            set
            {
                if (PositionRoom.Left == value)
                {
                    return;
                }
                PositionRoom.Left = value;
                RC.Current.SavePositionData();
                RaisePropertyChanged("Left");
            }
        }

        #endregion
        #region "Top"

        public Double Top
        {
            get
            {
                return PositionRoom.Top;
            }
            set
            {
                if (PositionRoom.Top == value)
                {
                    return;
                }
                PositionRoom.Top = value;
                RC.Current.SavePositionData();
                RaisePropertyChanged("Top");
            }
        }

        #endregion

        PositionRoom _PositionRoom;
        PositionRoom PositionRoom
        {
            get
            {
                if (_PositionRoom == null)
                {
                    _PositionRoom = RC.Current.PositionData.PositionRooms.SingleOrDefault(p => p.Id == this.Id);
                }
                return _PositionRoom;
            }
        }

        #endregion

        #region "检测结果"

        private ECheckFlag _检测结果 = ECheckFlag.未检测;

        public ECheckFlag 检测结果
        {
            get
            {
                return _检测结果;
            }
            set
            {
                if (_检测结果 == value)
                {
                    return;
                }

                var oldValue = _检测结果;
                _检测结果 = value;

                RaisePropertyChanged("检测结果");
                RaisePropertyChanged("检测结果的颜色");
            }
        }

        public SolidColorBrush 检测结果的颜色
        {
            get
            {
                switch (this.检测结果)
                {
                    case ECheckFlag.未检测:
                        return new SolidColorBrush(Colors.Yellow);
                    case ECheckFlag.成功:
                        return new SolidColorBrush(Colors.Green);
                    case ECheckFlag.失败:
                        return new SolidColorBrush(Colors.Red);
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        #endregion

        #region Copy

        public ICommand 重新加载拷贝温室信息命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        ReloadRoomTree();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return !IsRoomDataChanged && !IsDataChanged && !RC.Current.IsMonitoring;
                    });
            }
        }

        public ICommand 拷贝温室信息命令
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        CopyRooms();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return true;
                    });
            }
        }

        private void CopyRooms()
        {

            var items = RoomTree.SelectedItems
                .Where(p => p.Tag is RoomData)
                .Select(p => (RoomData)p.Tag)
                .Where(p => p.Id != this.Id)
                .ToList();

            items.ForEach(p =>
                {
                    AppUtility.CopyPropertyData(typeof(CopyMemberAttribute), this.roomData, p);
                    AppUtility.CopyPropertyData(typeof(MsgVMAttribute),
                        RC.Current.RootData.温室设置信息集合.Single(r => r.Id == this.Id),
                        RC.Current.RootData.温室设置信息集合.Single(r => r.Id == p.Id));
                });

            if (items.Count > 0)
            {
                RC.Current.SaveRootData();
                this.TagString = string.Format("成功复制{0}个信息", items.Count);
                Messenger.Default.Send<MessageBase>(new MessageBase(), "Reload温室实时数据集合");
            }
        }

        public TreeViewModel RoomTree { get; private set; }

        void ReloadRoomTree()
        {
            RoomTree.Items.Clear();
            var allNode = RoomTree.NewItem();
            allNode.Tag = "全部";
            allNode.Name = "全部";

            RoomTree.Items.Add(allNode);
            var groups = RC.Current.RootData.温室集合.GroupBy(p => p.RoomGroupName.Trim()).OrderBy(p => p.Key);
            foreach (var groupItem in groups)
            {
                var groupNode = RoomTree.NewItem();
                groupNode.Tag = groupItem.Key;
                groupNode.Name = groupItem.Key;
                allNode.Items.Add(groupNode);

                foreach (var roomItem in groupItem.OrderBy(p => p.RoomName))
                {
                    var roomNode = RoomTree.NewItem();
                    roomNode.Tag = roomItem;
                    groupNode.Items.Add(roomNode);
                }
            }
        }

        #endregion

    }
}
